let app = getApp(),integral_catId = 0, integral_index = -1;

Page({
    data: {
        gzShow:false,
    },
    onLoad: function(e) {
      let t = this,token = wx.getStorageSync('ACCESS_TOKEN')
      if(token){
        t.setData({token:token})
        wx.showLoading({title: '加载中',mask:true})
        t.getIntegral(token)
        t.setInitPage(t,e,token)
        wx.hideLoading()
      }else {
        app.args.page.onLoad(this, e)
      }
    },
    setInitPage: function(t,e,token) {
        app.args.request({
            url: app.args.api.siteinfo.sitelive + 'integral/goods',
            data:{
                storeId: app.args.api.siteinfo.hosId2,
                open_id:token
            },
            success(res){
                console.log(res)
                if(res.code == 0) {
                    let b = [], a = res.data, n = []
                    if (a.length > 0) for (var o in a) 0 < a[o].data.length && n.push(a[o])
                    if (0 < n.length) for (var i in n) {
                        var s = n[i].data
                        for (var r in s) b.push(s[r])
                    }
                    t.setData({
                        goods_list: a,
                        index_goods: b,
                        cat_checked: 0,
                        rule:res.rule
                    })
                }
            }
        })
    },
    goodsAll: function() {
        var t = this.data.goods_list, e = []
        for (var a in t) {
            var n = t[a].data
            for (var o in n) e.push(n[o])
        }
        this.setData({
            index_goods: e,
            cat_checked: 0
        });
    },
    catGoods: function(t) {
        let e = t.currentTarget.dataset, a = this.data.goods_list
        this.setData({
            index_goods: a[e.index].data,
            cat_checked: e.catId
        })
    },
    goodsInfo: function(t) {
        var e = t.currentTarget.dataset.goodsId;
        wx.navigateTo({
            url: "/pages/integral-mall/goods-info/index?goods_id=" + e
        });
    },
    getIntegral: function(t) {
        let _this = this
        app.args.request({
            url: app.args.api.siteinfo.sitelive + 'integral/info',
            data:{
                storeId: app.args.api.siteinfo.hosId2,
                //openId:t
                openId:'oSdH80GceCJqgJ-RVAIo_ETdQeoU'
            },
            success(res){
                res.code == 0 && _this.setData({integral: res.jf})
            }
        })
    },
    exchange: function(t) {
        let _this = this, info = t.currentTarget.dataset.index
        _this.data.integral > info.integral ? app.showError('当前积分不足') : wx.showModal({
            title: '提示',
            content: '是否兑换',
            success (re) {
                re.confirm && wx.request({
                    header: {'content-type': 'application/json'},
                    url: app.args.api.siteinfo.sitelive + 'integral/exchange',
                    data:{
                        storeId: app.args.api.siteinfo.hosId2,
                        openId:_this.data.token,
                        goodsId:info.id
                    },
                    success(res){
                        if(res.code == 0) {
                            _this.setData({integral:_this.data.integral-info.integral})
                            wx.showModal({
                                title: '兑换成功',
                                content: info.name,
                                cancelText:'继续兑换',
                                confirmText:'立即使用',
                                success (r) {
                                    r.confirm && _this.orderList()
                                }
                            })
                        }else{
                            app.showError('当前积分不足')
                        }
                    }
                })
            }
        })
    },
    orderList:function(){
        wx.navigateTo({
            url: "/pages/integral-mall/order/index"
        })
    },
    onReady: function(t) {},
    onShow: function(t) {},
    onHide: function(t) {},
    onUnload: function(t) {},
    onPullDownRefresh: function(e) {
        this.onLoad()
        wx.stopPullDownRefresh()
    },
    onShareAppMessage() {
        // 构建分享参数
        return {
          title: "积分商城",
          path: "/pages/integral-mall/index/index?" + App.getShareUrlParams()
        };
    },
    gzShow(){
        this.setData({gzShow:!this.data.gzShow})
    }
});